/**
 * This file is part of ${project_name}.
 * 
 * ${project_name} is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * 
 * ${project_name} is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * ${project_name}. If not, see <http://www.gnu.org/licenses/>.
 * 
 * @see http://www.gnu.org/licenses/lgpl.txt
 * @author art <lijianghui2000@gmail.com>
 */
package com.life.android.audiotageditor.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.life.android.audiotageditor.R;
import com.life.android.audiotageditor.audio.AudioTag;
import com.life.android.audiotageditor.model.AudioFile;
import com.life.android.audiotageditor.model.AudioFileInfo;
import com.life.android.audiotageditor.model.AudioModel;
import com.life.android.audiotageditor.model.AudioModelManager;
import com.life.android.audiotageditor.util.AudioFileUtil;

public class Audio extends Activity implements OnClickListener {

	private AudioModel audioModel = (AudioModel) AudioModelManager.instance()
			.getRoot();

	private AudioFile audioFile;

	private TextView nameText;
	private EditText titleEdit;
	private EditText artistEdit;
	private EditText albumEdit;
	private EditText yearEdit;
	private EditText trackEdit;
	private EditText genreEdit;
	private EditText commentEdit;
	private EditText albumArtistEdit;
	private Button saveButton;
	private Button cancelButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.audio);

		String audioFilePath = getIntent().getStringExtra("audioFilePath");
		if (audioFilePath == null || audioFilePath.length() <= 0) {
			return;
		}
		audioFile = (AudioFile) audioModel.getFile(audioFilePath);
		AudioTag audioTag = ((AudioFileInfo) (audioFile).getAudioModelInfo())
				.getAudioTag();

		nameText = (TextView) findViewById(R.id.nameText);
		nameText.setText(audioFile.getName());
		titleEdit = (EditText) findViewById(R.id.titleEdit);
		titleEdit.setText(audioTag.getTitle());
		artistEdit = (EditText) findViewById(R.id.artistEdit);
		artistEdit.setText(audioTag.getArtist());
		albumEdit = (EditText) findViewById(R.id.albumEdit);
		albumEdit.setText(audioTag.getAlbum());
		yearEdit = (EditText) findViewById(R.id.yearEdit);
		yearEdit.setText(audioTag.getYear());
		trackEdit = (EditText) findViewById(R.id.trackEdit);
		trackEdit.setText(audioTag.getTrack());
		genreEdit = (EditText) findViewById(R.id.genreEdit);
		genreEdit.setText(audioTag.getGenre());
		commentEdit = (EditText) findViewById(R.id.commentEdit);
		commentEdit.setText(audioTag.getComment());
		albumArtistEdit = (EditText) findViewById(R.id.albumArtistEdit);
		albumArtistEdit.setText(audioTag.getAlbumArtist());

		saveButton = (Button) findViewById(R.id.saveButton);
		saveButton.setOnClickListener(this);
		cancelButton = (Button) findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View paramView) {
		switch (paramView.getId()) {
		case R.id.saveButton:
			if (audioFile != null) {
				AudioTag audioTag = new AudioTag();
				audioTag.setTitle(titleEdit.getText().toString());
				audioTag.setArtist(artistEdit.getText().toString());
				audioTag.setAlbum(albumEdit.getText().toString());
				audioTag.setYear(yearEdit.getText().toString());
				// TODO
				audioTag.setTrack("0");
				audioTag.setGenre(genreEdit.getText().toString());
				audioTag.setComment(commentEdit.getText().toString());
				audioTag.setAlbumArtist(albumArtistEdit.getText().toString());
				AudioFileUtil.saveAudioFile(audioFile.getFullPath(), audioTag);
			}
			finish();
			break;
		case R.id.cancelButton:
			finish();
			break;
		}

	}
}